from django.http import HttpResponse, HttpResponseBadRequest,\
    HttpResponseNotFound
import logging
import os
import shutil

log = logging.getLogger()

def transmision(request):
    try:
        if request.method == 'GET':
            return HttpResponse("Przyszedl GET cieszmy sie.")
        elif request.method == 'POST':
            #jezeli nie bedzie filt to bedzie opis zmiany dostosowany do katologow i usuniecia plikow.
            d = 'D:/magisterka_folder_rec/' + request.POST.get("root")
            
            if request.POST.get("action") == "delete":
                print "HERE I COME WITH DELETE!" + d
                if os.path.exists(d):
                    if os.path.isfile(d):
                        os.remove(d)
                    if os.path.isdir(d+"/"):
                        shutil.rmtree(d)
            else:
                incFile = request.FILES['file']
                if not os.path.exists(d):
                    os.makedirs(d)
                destination = open( d + incFile.name, 'wb+')
                for chunk in incFile.chunks():
                    destination.write(chunk)
                destination.close()
            
            return HttpResponse("DZIALA")
    except Exception, ex:
        log.exception('wyjatek : %s', str(ex))
        return HttpResponse("NIE DZIALA")

def authentication(request):
    try:
        if request.method == 'GET':
            #print 'AUTH GET. ACC-> ' + request.GET.get('acc') + ' PASS->' + request.GET.get("pass")

            #poleczenie z baza i czy jest ok.

            return HttpResponse("OK")
        else :
            return HttpResponseBadRequest()
    except Exception, ex:
        log.exception('wyjatek : %s', str(ex))
        return HttpResponse("NIE DZIALA")

def generateSessionKey(request):
    try:
        if request.method == 'GET':
            print 'Trwa generowanie klucza sesji dla uzytkownika ' + request.GET.get("acc")
            return HttpResponse("eeeeeeeeAAAA")
        else :
            return HttpResponseBadRequest()
    except Exception, ex:
        log.exception('wyjatek : %s', str(ex))
        return HttpResponse("NIE DZIALA")

def setCookieWithSessionKey(request):
    try:
        if request.method == 'GET':
            print 'setCookieWithSessionKey'
            #XXX getSessionKey
            response = HttpResponse()
            response.set_cookie('cookie_name', 'cookie_value')
            return response
        else :
            return HttpResponseBadRequest()
    except Exception, ex:
        log.exception('wyjatek : %s', str(ex))
        return HttpResponse("NIE DZIALA")

def retriveCookieSessionKey(request):
    try:
        print 'hello'
        if request.method == 'POST':
            #XXX checkSessionKey
            #XXX czy cookie klucz przesylac do sprawdzenia czy wystarczy sessionKey
#            if request.COOKIES.has_key( 'acc' ):
#                value = request.COOKIES[ 'acc' ]
#                print value

            #print 'AUTH POST. ACC-> ' + request.POST.get('acc') + ' PASS->' + request.GET.get("pass")

            return HttpResponse('OK')
        else :
            return HttpResponseBadRequest()
    except Exception, ex:
        log.exception('wyjatek : %s', str(ex))
        return HttpResponse("NIE DZIALA")
